package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.ArchiveDefectReq;
import com.ruibang.glass.quality.domain.req.EverydayArchiveDefectReq;
import com.ruibang.glass.quality.domain.resp.ReinvestmentInfo;
import com.ruibang.glass.quality.entity.EverydayArchiveDefect;
import com.ruibang.glass.quality.mapper.EverydayArchiveDefectMapper;
import com.ruibang.glass.quality.service.EverydayArchiveDefectService;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 每日归档缺陷统计信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2023-12-13
 */
@Service
public class EverydayArchiveDefectServiceImpl extends ServiceImpl<EverydayArchiveDefectMapper, EverydayArchiveDefect> implements EverydayArchiveDefectService {


    @Override
    public List<ReinvestmentInfo> getDayReinvestmentNum(ArchiveDefectReq archiveDefectReq){
        return baseMapper.getDayReinvestmentNum(archiveDefectReq);
    }

    @Override
    public void deleteByParam(ArchiveDefectReq archiveDefectReq) {
        baseMapper.deleteByParam(archiveDefectReq);
    }

    @Override
    public List<EverydayArchiveDefect> queryEverydayArchiveDefects(EverydayArchiveDefectReq defectReq) {
        final LambdaQueryWrapper<EverydayArchiveDefect> lambda = new QueryWrapper<EverydayArchiveDefect>().lambda();
        this.buildCondition(lambda,defectReq);
        return this.list(lambda);
    }

    @Override
    public PageResult<EverydayArchiveDefect> queryPage(PageRequest<EverydayArchiveDefectReq> pageRequest) {
        final LambdaQueryWrapper<EverydayArchiveDefect> lambda = new QueryWrapper<EverydayArchiveDefect>().lambda();
        this.buildCondition(lambda,pageRequest.getParam());
        final IPage<EverydayArchiveDefect> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        return new PageResult(page);
    }

    @Override
    public List<EverydayArchiveDefect> queryEveryDayByTime(ArchiveDefectReq archiveDefectReq) {
        return baseMapper.queryEverydayByTime(archiveDefectReq);
    }

    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<EverydayArchiveDefect> lambda, EverydayArchiveDefectReq param){
        if(null == param) return;
        if(param.getStartTime()!=null && param.getEndTime()!=null){
            lambda.ge(EverydayArchiveDefect::getDefectTime, param.getStartTime());
            lambda.le(EverydayArchiveDefect::getDefectTime, param.getEndTime());
        }
        if(!StringUtils.isEmpty(param.getCheckMachineCode())){
            lambda.eq(EverydayArchiveDefect::getCheckMachineCode, param.getCheckMachineCode());
        }
        lambda.orderBy(true,false, EverydayArchiveDefect::getDefectTime);
    }
}
